package com.ybb.production.service.impl;

import com.yubb.common.utils.DateUtils;
import com.ybb.production.domain.GarmentProductionUserPayTemplate;
import com.ybb.production.dto.GarmentProductionPayDetailParam;
import com.ybb.production.dto.GarmentProductionSettleParam;
import com.ybb.production.mapper.GarmentProductionUserPayTemplateMapper;
import com.ybb.production.responce.GarmentPayDetailResp;
import com.ybb.production.service.IGarmentProductionUserPayService;
import com.ybb.production.service.IGarmentProductionUserPayTemplateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 工资发放模板Service业务层处理
 *
 * @author dengjie
 * @date 2023-09-28
 */
@Service
public class GarmentProductionUserPayTemplateServiceImpl implements IGarmentProductionUserPayTemplateService {
    @Autowired
    private GarmentProductionUserPayTemplateMapper garmentProductionUserPayTemplateMapper;

    @Autowired
    private IGarmentProductionUserPayService garmentProductionUserPayService;

    /**
     * 查询工资发放模板
     *
     * @param id 工资发放模板主键
     * @return 工资发放模板
     */
    @Override
    public GarmentProductionUserPayTemplate selectGarmentProductionUserPayTemplateById(Long id) {
        return garmentProductionUserPayTemplateMapper.selectGarmentProductionUserPayTemplateById(id);
    }

    /**
     * 查询工资发放模板列表
     *
     * @param garmentProductionUserPayTemplate 工资发放模板
     * @return 工资发放模板
     */
    @Override
    public List<GarmentProductionUserPayTemplate> selectGarmentProductionUserPayTemplateList(GarmentProductionUserPayTemplate garmentProductionUserPayTemplate) {
        return garmentProductionUserPayTemplateMapper.selectGarmentProductionUserPayTemplateList(garmentProductionUserPayTemplate);
    }

    /**
     * 新增工资发放模板
     *
     * @param garmentProductionUserPayTemplate 工资发放模板
     * @return 结果
     */
    @Override
    public int insertGarmentProductionUserPayTemplate(GarmentProductionUserPayTemplate garmentProductionUserPayTemplate) {
        garmentProductionUserPayTemplate.setCreateTime(DateUtils.getNowDate());
        return garmentProductionUserPayTemplateMapper.insertGarmentProductionUserPayTemplate(garmentProductionUserPayTemplate);
    }

    /**
     * 修改工资发放模板
     *
     * @param garmentProductionUserPayTemplate 工资发放模板
     * @return 结果
     */
    @Override
    public int updateGarmentProductionUserPayTemplate(GarmentProductionUserPayTemplate garmentProductionUserPayTemplate) {
        garmentProductionUserPayTemplate.setUpdateTime(DateUtils.getNowDate());
        return garmentProductionUserPayTemplateMapper.updateGarmentProductionUserPayTemplate(garmentProductionUserPayTemplate);
    }

    /**
     * 批量删除工资发放模板
     *
     * @param ids 需要删除的工资发放模板主键
     * @return 结果
     */
    @Override
    public int deleteGarmentProductionUserPayTemplateByIds(Long[] ids) {
        return garmentProductionUserPayTemplateMapper.deleteGarmentProductionUserPayTemplateByIds(ids);
    }

    /**
     * 删除工资发放模板信息
     *
     * @param id 工资发放模板主键
     * @return 结果
     */
    @Override
    public int deleteGarmentProductionUserPayTemplateById(Long id) {
        return garmentProductionUserPayTemplateMapper.deleteGarmentProductionUserPayTemplateById(id);
    }

    @Override
    public int insertOrUpdate(GarmentProductionUserPayTemplate param) {
        GarmentProductionUserPayTemplate qry = new GarmentProductionUserPayTemplate();
        qry.setUserId(param.getUserId());
        List<GarmentProductionUserPayTemplate> list = garmentProductionUserPayTemplateMapper.selectGarmentProductionUserPayTemplateList(qry);
        param.setDelFlag("0");
        if (CollectionUtils.isEmpty(list)) {
            garmentProductionUserPayTemplateMapper.insertGarmentProductionUserPayTemplate(param);
        } else {
            param.setId(list.get(0).getId());
            garmentProductionUserPayTemplateMapper.updateGarmentProductionUserPayTemplate(param);
        }
        return 1;
    }

    @Override
    public int batchSettlementByUser(GarmentProductionPayDetailParam detailParam) {
        if (detailParam == null || CollectionUtils.isEmpty(detailParam.getUserIds())) {
            return 0;
        }
        // 查询未结算的编菲
        detailParam.setStatus(2);
        List<GarmentPayDetailResp> resps = garmentProductionUserPayService.settlementDetailViewList(detailParam);
        if (CollectionUtils.isEmpty(resps)) {
            return 0;
        }
        List<Long> collect = resps.stream().map(GarmentPayDetailResp::getId).collect(Collectors.toList());
        GarmentProductionSettleParam param = new GarmentProductionSettleParam();
        param.setIds(collect);
        param.setSalaryTime(detailParam.getSalaryTime());
        return garmentProductionUserPayService.batchSettlement(param);
    }
}
